package it.win4buy.controllers
{
    import flash.events.IEventDispatcher;
    import flash.events.TimerEvent;
    import flash.utils.Timer;

    import it.win4buy.events.GetPlaceHolderEvent;
    import it.win4buy.events.UtilityEvent;
    import it.win4buy.models.ApplicationModel;
    import it.win4buy.services.GetPlaceHolderService;
    import it.win4buy.utility.ApplicationConstants;
    import it.win4buy.utility.LoggerUtilty;

    /**
     *	Class controls if there are changes in the placeholder list.<br>
     *	autore:
     *  pierangelo.mancusi@gmail.com
     *
     */

    public class PollingController
    {



        //-------------------------------------------
        //
        // Injected properties
        //
        //-------------------------------------------

        [Dispatcher]
        public var dispatcher:IEventDispatcher;


        [Inject]
        public var applicationModel:ApplicationModel;

        [Inject]
        public var log:LoggerUtilty;

        [Inject]
        public var getPlaceHolderService:GetPlaceHolderService;

        //-------------------------------------------
        //
        // Private Properties
        //
        //-------------------------------------------

        /**
        * a simple counter
        */
        private var i:int=0;


        private var timer:Timer;

        //-------------------------------------------
        //
        // Getters / Setters
        //
        //-------------------------------------------


        //-------------------------------------------
        //
        // Constructor
        //
        //-------------------------------------------

        public function PollingController()
        {


        }



        public function init():void
        {
            log.logToConsole(this, "Polling system started.");

            //sets the time interval in milliseconds
            //5000 = 5sec
            timer=new Timer(ApplicationConstants.POLLING_GET_PLACEHOLDER_INTERVAL, 0);


            startPlaceHolderPolling();

        }


        //-------------------------------------------
        //
        // Private Methods
        //
        //-------------------------------------------

        private function startPlaceHolderPolling():void
        {

            if (applicationModel.applicationState == 0)
            {
                // application in PlaceHolder MODE

                timer.addEventListener(TimerEvent.TIMER, getPlaceHolderList);
                timer.start();
            }


        }


        private function getPlaceHolderList(event:TimerEvent):void
        {
            i=i + 1;
            log.logToConsole(this, "polling trying n. [ " + i + " ]");
            getPlaceHolderService.execute();



        }

        /**
         * Force Refreshing DataBinding in the application
         *
         */
        private function refreshDataBinding():void
        {

            dispatcher.dispatchEvent(new UtilityEvent(UtilityEvent.REFRESH_BINDING, null, true));

        }

        //-------------------------------------------
        //
        // Mediate Methods
        //
        //-------------------------------------------


        [EventHandler(event="GetPlaceHolderEvent.RESULT", priority="990")]
        [EventHandler(event="GetPlaceHolderEvent.FAULT", priority="990")]
        public function placeHolderResult(event:GetPlaceHolderEvent):void
        {
            switch (event.type)
            {
                case GetPlaceHolderEvent.RESULT:
                    log.logToConsole(this, "placeHolderResult(): event.type = GetPlaceHolderEvent.RESULT");

                    applicationModel.placeHolderList=event.placeHolder;

                    break;

                case GetPlaceHolderEvent.FAULT:
                    log.logToConsole(this, "placeHolderResult(): event.type= GetPlaceHolderEvent.FAULT");

                    break;
            }


            //aggiorna i bindings
            refreshDataBinding();
        }




    }
}